/**
 * @file instprofit/Home.js
 *  实例盈亏
 * @author
 */


 import React, {Component, PropTypes} from 'react';
 import {connect} from 'dva';
 import {routerRedux, Link, withRouter} from 'dva/router';
 import {
	 Button,
	 Modal,
	 Row,
	 Col,
	 Table,
	 Icon,
	 Card,
	 Form,
	 message
 } from 'antd';
 import moment from 'moment';
 import Tabtitle from '../../../components/common/TabTitle';
 import Tablelist from '../../../components/common/TableData';
 // import DateRangePicker from '../component/searchBar/Datesearch';
 import DateRangePicker from '../component/searchBar/DateSearchBar';
 import styles from './home.less';


 const nowDate = moment(new Date()).format('YYYYMMDD');
 const params = {
	 table: 'ifinstprofit',
	 pagesize: 15,
	 currentpage: 1
 };
 const Instprofit = ({
	 dispatch,
	 instprofit,
	 location: {
		 pathname,
		 query
	 }
 }) => {
	 if (instprofit.status) {
		 const {instprofitlist,currentpage, page, state, begindate, enddate,msg} = instprofit;
		 console.log('instprofit>>>>', instprofit,);
		 const tag = "instprofit";
		 const columns = [
	{
		title: '交易日期',
		dataIndex: 'tradeDay',
		key: 'tradeDay',
		sorter: true,
		
	},
	{
		title: '基金号',
		dataIndex: 'fundId',
		key: 'fundId',
		
	},
	{
		title: '组合号',
		dataIndex: 'combiNo',
		key: 'combiNo',
		
	},
	{
		title: '组合名称',
		dataIndex: 'combiName',
		key: 'combiName',
		
	},
	{
		title: '实例号',
		dataIndex: 'instanceId',
		key: 'instanceId',
		sorter: true,
		
	},
	{
		title: '实例总盈亏',
		dataIndex: 'totalProfit',
		key: 'totalProfit',
		
	},
	{
		title: '基差总盈亏',
		dataIndex: 'basisSpreadProfit',
		key: 'basisSpreadProfit',
		
	},

	{
		title: '贝塔盈亏',
		dataIndex: 'betaProfit',
		key: 'betaProfit',
		
	},
	{
		title: '成本',
		dataIndex: 'cost',
		key: 'cost',
		
	},
	{
		title: '阿尔法盈亏',
		dataIndex: 'alphaProfit',
		key: 'alphaProfit',
		
	},
	{
		title: '阿尔法盈亏+成本',
		dataIndex: 'alphaAndCost',
		key: 'alphaAndCost',
		
	},
	{
		title: '空头市值',
		dataIndex: 'totalShortValue',
		key: 'totalShortValue',
		
	},
	{
		title: '多头市值',
		dataIndex: 'totalLongValue',
		key: 'totalLongValue',
		
	},
	{
		title: '调整项',
		dataIndex: 'adjust',
		key: 'adjust',
		
	},
];
		 // 分页
		 function onShowSizeChange(current, pagesize) {
			 dispatch({
				 type: 'instprofit/changeRouter',
				 payload: {
					 table: 'ifinstprofit',
					 pagesize,
					 currentpage:1,
					 begindate: begindate,
					 enddate: enddate,
					 sortField: query.sortField,
					 sortOrder: query.sortOrder,
				 }
			 })
		 }
		 function onChangecurrentpage(current,pagesize) {
			 dispatch({
				 type: 'instprofit/changeRouter',
				 payload: {
					 table: 'ifinstprofit',
					 currentpage: current,
					 pagesize:pagesize,
					 begindate: begindate,
					 enddate: enddate,
					 sortField: query.sortField,
						sortOrder: query.sortOrder,
				 }
			 })
		 }
		 //排序
		function onSorterChange(pagesize, filters, sorter) {
		 	const orderField = sorter.field;
		 	const orderSort = sorter.order;
		 	console.log(sorter, "orderField>>", orderField, "orderSort", orderSort);
		 	if(orderField !== undefined && orderSort !== undefined){
		 		if(query.pagesize&&query.begindate&&query.enddate){
			 		dispatch({
					 	type: 'instprofit/changeRouter',
					 	payload: {
						 	table: 'ifinstprofit',
						 	currentpage: currentpage,
							pagesize:query.pagesize,
							sortField: orderField,
							sortOrder: orderSort,
							begindate: begindate,
						 	enddate: enddate
					 	}
				 	})
			 	}else{
			 		dispatch({
					 	type: 'instprofit/changeRouter',
					 	payload: {
						 	table: 'ifinstprofit',
						 	currentpage: currentpage,
							pagesize:15,
						 	begindate,
						 	enddate,
						 	sortField: orderField,
						 	sortOrder: orderSort,
						
					 	}
				 	})
			 	}
		 	}else {
		 		return null
		 	}
			 
			 
		}
		//导出
		function downLoad(){
			const nowBegindate = begindate || moment(new Date()).format('YYYYMMDD');
			const nowEnddate = enddate || moment(new Date()).format('YYYYMMDD');
      window.location.href=location.href.indexOf('localhost')>-1?`/api/ifdata/history/export?table=ifinstprofit&begindate=${nowBegindate}&enddate=${nowEnddate}`:`/thfx/api/ifdata/history/export?table=ifinstprofit&begindate=${nowBegindate}&enddate=${nowEnddate}`;
		}
		function queryData() {
		console.log('beginDate>>>',begindate,'endDate>>>',enddate)
		if(begindate == "" || enddate == "" || begindate == "Invalid date"){
			message.warning('请选择时间');
			return;
		}else{
			dispatch({
				type: `${tag}/changeRouter`,
				payload: {
					table:`if${tag}`,
					pagesize:15,
					currentpage:1,
					begindate:begindate || BD,
					enddate:enddate || ED
				}
			})
			dispatch({
				type: `${tag}/setState`,
				payload: {
					'isRefresh':0
				}
			})
		}
	}
		 return (
			 <div>
				 <Tabtitle value={'实例盈亏'}/>
				 <div className="page-instprofit-home content-inner">
					 <Row className={styles.toolbar} type="flex" justify="start" align="middle">
						 <Col span={4}>
							 <DateRangePicker tag={tag} dispatch={dispatch} begindate={begindate} enddate={enddate} location={location}/>
						 </Col>
						 <Col span={18} push={2}>
							<Button type="primary" onClick={queryData}>
			          查询
			        </Button>
						</Col>
						 <Col span={2}>
							 <Button type="primary" onClick={downLoad}>导出</Button>
						 </Col>
					 </Row>
						<Tablelist columns={columns} data={instprofitlist} page={page} onShowSizeChange={onShowSizeChange} onChangecurrentpage={onChangecurrentpage} onSorterChange={onSorterChange} record={record => `${record.instanceId}-${record.fundId}-${record.combiNo}-${record.stockCode}-${record.contractType}-${record.endinstprofitAmount}-${record.tradeDay}-${record.basisSpreadProfit}-${record.eodBasisSpread}-${record.totalCloseAmount}-${record.combiName}-${record.totalOpenAmount}-${record.totalProfit}-${record.betaProfit}-${record.cost}-${record.alphaProfit}-${record.alphaAndCost}-${record.totalShortValue}-${record.totalLongValue}`}/>
				 </div>
			 </div>
		 )
	 } else {
		 return null
	 }
 }

 Instprofit.propTypes = {}
 export default connect(({instprofit}) => ({instprofit}))(Instprofit)
